package org.example.trans;
import java.util.*;
import org.example.model.*;

/**
 * @author lenovo
 */

public class TransUser {
    //将ArrayList<ArrayList<String>>转为HashMap<String,User>
    public static HashMap<String,User> transUserRead
            (ArrayList<ArrayList<String>> arrayListOut){
        HashMap<String,User> hashMap = new HashMap<>();
        if(arrayListOut == null){
            return null;
        }
        for (ArrayList<String> arrayListIn : arrayListOut){
            User user=new User();
            user.setUsername(arrayListIn.get(0));
            user.setPassword(arrayListIn.get(1));
            hashMap.put(user.getUsername(),user);
        }
        return hashMap;
    }
    //将HashMap<String,User>转为ArrayList<ArrayList<String>>
    public static ArrayList<ArrayList<String>> transUserWrite
            (HashMap<String,User> hashMap){
        ArrayList<ArrayList<String>> arrayListOut = new ArrayList<>();
        if (hashMap == null){
            return null;
        }
        for (Map.Entry<String,User> map : hashMap.entrySet()){
            ArrayList<String> arrayListIn = new ArrayList<>();
            arrayListIn.add(map.getKey());
            arrayListIn.add(map.getValue().getPassword());
            arrayListOut.add(arrayListIn);
        }
        return arrayListOut;
    }
}
